package com.lin.views;


import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lin.utils.WindowUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class GIFView extends ImageView implements Runnable{
	private Bitmap mBitmap;
	private GIFDecode mGifDecode;
	private boolean isGif;
//	private int ind;
//	private int gifCount;
//	Thread t;
	private DisplayMetrics mDisplayMetrics;
	private int mScreenWid , mScreenHei;
	private ExecutorService mExecutorService;
	private long mGifDuration = 300;
	 public GIFView(android.content.Context context){
		 super(context);
		 initView();
	 }

	public GIFView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	public GIFView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	private void initView(){
		mDisplayMetrics = WindowUtil.getWindowManger(getContext()).getDisplayMetrics();
		mScreenWid = mDisplayMetrics.widthPixels;
		mScreenHei = mDisplayMetrics.heightPixels;
//		mExecutorService = Executors.newSingleThreadExecutor();
	}
	public void setGifInputStream(InputStream inputStream) {
		
		startPlayGif(inputStream);
	}
	public void setImageBitmap(Bitmap bitmap) {
		mBitmap = bitmap;
		isGif = false;
		invalidate();
	}
	private void startPlayGif(InputStream inputStream) {
		isGif = true;
		 mGifDecode=new GIFDecode();
			mGifDecode.read(inputStream);
//			 ind = 0;
			 //decode.
//				gifCount = decode.getFrameCount();
			 mBitmap=mGifDecode.getFrame(0);
			 if (mExecutorService == null) {
				mExecutorService = Executors.newFixedThreadPool(3);
			}
//			 else if (!mExecutorService.isShutdown()) {
//				mExecutorService.shutdownNow();
//			}
		mExecutorService.execute(this);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		if (mBitmap != null) {
			int bitmapWid = mBitmap.getWidth();
			int bitmapHei = mBitmap.getHeight();
			
			float scale = mScreenWid * 1.0f/ bitmapWid;
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			Bitmap bitmap = Bitmap.createBitmap(mBitmap, 0, 0, bitmapWid, bitmapHei, matrix, true);
//			if (!mBitmap.isRecycled()) {
//				mBitmap.recycle();
//			}
//			Log.i("tag", "-----------scale--------------" + scale);
			canvas.drawBitmap(bitmap , 0,(mScreenHei - bitmap.getHeight()) / 2,null);
			if (isGif) {
				mBitmap=mGifDecode.next();
			}
		}
	}

	@Override
	public void run() {
		   while(isRun){
		    	  try{
		    		  this.postInvalidate();
		    		Thread.sleep(300) ; 
		    		
		    	  }catch(Exception ex){
		    		  
		    	  }
		      }
		
	}
	private boolean isRun = true;
	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		if (mExecutorService != null && !mExecutorService.isShutdown()) {
			isRun = false;
			mExecutorService.shutdownNow();
		}
	}

	public long getGifDuration() {
		return mGifDuration;
	}

	public void setGifDuration(long mGifDuration) {
		this.mGifDuration = mGifDuration;
	}
	
	
}
